package com.etennis.controller;

import com.etennis.pojos.ResponseResult;
import com.etennis.pojos.message.ExchangeMessage;
import com.etennis.pojos.vo.app.CommodityVO;
import com.etennis.pojos.vo.app.ExchangeVO;
import com.etennis.pojos.vo.app.OrderVO;
import com.etennis.pojos.vo.app.UserVO;
import com.etennis.service.IMemberService;
import com.etennis.service.IOrderService;
import com.etennis.utils.SecurityUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/exchange")
@Slf4j
@Api(tags = "用户端-积分兑换接口")
public class ExchangeController {
    @Autowired
    IMemberService iMemberService;
    @Autowired
    IOrderService iOrderService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    private static final ObjectMapper objectMapper = new ObjectMapper();


    @GetMapping("/mine")
    @ApiOperation("个人信息")
    public ResponseResult<UserVO> mine(){
        UserVO mine = iMemberService.mine();
        return ResponseResult.okResult(mine);
    }

    @GetMapping("/commodityList")
    @ApiOperation("展示商品列表")
    public ResponseResult<List<CommodityVO>> commodityList(){
        List<CommodityVO> list=iOrderService.commodity();
        return ResponseResult.okResult(list);
    }

    @GetMapping("/orderList")
    @ApiOperation("展示兑换记录")
    public ResponseResult<List<OrderVO>> orderList(){
        List<OrderVO> list=iOrderService.orderList();
        return ResponseResult.okResult(list);
    }

    @GetMapping("/exchange/{id}")
    @ApiOperation("确认兑换")
    public ResponseResult exchange(@PathVariable Long id) throws JsonProcessingException {
        rabbitTemplate.convertAndSend("demo.exchange","",objectMapper.writeValueAsString(new ExchangeMessage(id, SecurityUtils.getLoginUserId())));
        return ResponseResult.okResult();
    }
}